package com.ioftv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ioftv.entity.ProjectInfo;
import com.ioftv.mapper.ProjectInfoMapper;
import com.ioftv.service.ProjectInfoService;
import com.ioftv.service.base.BaseService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目信息Service实现
 */
@Service
public class ProjectInfoServiceImpl extends BaseService<ProjectInfoMapper, ProjectInfo> implements ProjectInfoService {

    @Override
    public List<ProjectInfo> getByProjectStatus(Integer projectStatus, Integer limit) {
        LambdaQueryWrapper<ProjectInfo> wrapper = createUserQueryWrapper();
        
        if (projectStatus != null) {
            wrapper.eq(ProjectInfo::getProjectStatus, projectStatus);
        }
        
        wrapper.orderByDesc(ProjectInfo::getCreateTime);
        
        if (limit != null && limit > 0) {
            wrapper.last("LIMIT " + limit);
        }
        
        return this.list(wrapper);
    }
    
    @Override
    public List<ProjectInfo> getByProjectStatusAndUserId(Integer projectStatus, Integer limit, Long userId) {
        LambdaQueryWrapper<ProjectInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProjectInfo::getUserId, userId);
        
        if (projectStatus != null) {
            wrapper.eq(ProjectInfo::getProjectStatus, projectStatus);
        }
        
        wrapper.orderByDesc(ProjectInfo::getCreateTime);
        
        if (limit != null && limit > 0) {
            wrapper.last("LIMIT " + limit);
        }
        
        return this.list(wrapper);
    }

    @Override
    protected void addUserCondition(LambdaQueryWrapper<ProjectInfo> queryWrapper) {
        queryWrapper.eq(ProjectInfo::getUserId, getCurrentUserId());
    }

    @Override
    protected void setEntityUserId(ProjectInfo entity) {
        if (entity.getUserId() == null) {
            entity.setUserId(getCurrentUserId());
        }
    }
}